<template>
    <div>
        <a-modal
            v-model="visible"
            :maskClosable="false"
            :title="(form.id?'编辑' : '新增') + '预算申报计划'">
                <a-form
                    v-if="visible"
                    ref="form"
                    layout="inline"
                    class="budgetDeclaration-plan-model-form-box"
                    :form="form"
                    >
                    <a-form-item label="计划名称" >
                        <a-input
                            v-model="formState.fjhmc"
                            placeholder="请输入计划名称"
                            v-decorator="['fjhmc',{ initialValue:formState.fjhmc, rules: rules.fjhmc}]"
                            />
                    </a-form-item>
                </a-form>
                <template slot="footer">
                    <a-button v-if="formState.id" class="blue-btn" @click="onSubmit">保存编辑并关闭弹窗</a-button>
                    <a-button v-else class="blue-btn" @click="onSubmit">确认新增并关闭弹窗</a-button>
                </template>
        </a-modal>
    </div>
</template>
<script>
import {
    yssbAdd,
    yssbEdit
} from './service/index'
export default {
    data() {
        return {
            visible: false,
            form:this.$form.createForm(this, { name: 'form' }),
            formState: {},
            rules:{
                fjhmc: [{
                    required: true,
                    message: '请输入计划名称',
                    trigger: 'blur',
                }],
            }
        };
    },
    methods: {
        getData(val) {
            this.formState = val
            this.visible = true;
        },
        onSubmit(){
            this.form.validateFields((err, values) => {
                if (!err) {
                    var that = this
                    that.$confirm({
                        title: that.formState.id?'您确定要修改当前计划名称吗？':'您确定要新增当前计划吗？',
                        onOk() {
                            let data = that.formState
                            if(that.formState.id){
                                yssbEdit(data).then(res => {
                                    if(res.success){
                                        that.$message.success(res.message)
                                        that.handleClose()
                                    } else {
                                        that.$message.warning(res.message)
                                    }
                                })
                            } else {
                                data = {
                                    ...data,
                                    fjhzt: '计划中',
                                    id:''
                                }
                                yssbAdd(data).then(res => {
                                    if(res.success){
                                        that.$message.success(res.message)
                                        that.handleClose()
                                    } else {
                                        that.$message.warning(res.message)
                                    }
                                })
                            }
                        }
                    })
                }
            })
        },
        handleClose() {
            this.visible = false;
            this.$emit('success')
        },
    },
};
</script>
<style lang="less">
    .budgetDeclaration-plan-model-form-box{
        .ant-form-item-label{
            label{
                display: inline-block;
                vertical-align: middle;
                margin-right: 5px;
                background-color: #e6f7ff;
                height: 32px;
                line-height: 32px;
                font-weight: 600;
                font-family: simHei;
                padding: 0 10px;
            }
        }
    }
</style>
